##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  figure_ii_iii.R 
#  -- file produces the plots of expert opinion in appendix figure ii and figure iii
#
##########################################################

rm(list=ls())  

library(foreign)
library(boot)
library(gplots)
library(ggplot2)
library(car)
library(survey)
library(grDevices)
library(plotrix)
                                            
load('~/Dropbox/Seeing_Spots/replication/expert_opinion.Rdata')

PLOT <- subset(DATA, subset=Q3!="NA")
colour <- "tan"
ggplot(PLOT, aes(Q3)) + geom_bar(fill=colour) + scale_x_discrete("If you had to guess, would you expect rates of skipping to be higher for negative or positive ads?", labels = c("Negative" = "Negative","Positive" = "Positive", "No Difference" = "No Difference")) + ylab("Count") + theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank())
dev.print(pdf, file="~/Dropbox/Seeing_Spots/replication/figures/ExpertNegPos.pdf", width=8, height=4, pointsize=10)
                                     

PLOT <- subset(DATA, subset=Q4!="NA")
colour <- "tan"
ggplot(PLOT, aes(Q4)) + geom_bar(fill=colour) + scale_x_discrete("...would you expect rates of skipping to be higher for ads from the respondent's party or the other party?", labels = c("Respondent's Party" = "Respondent's Party","Other Party" = "Other Party", "No Difference" = "No Difference")) + ylab("Count") + theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank())
dev.print(pdf, file="~/Dropbox/Seeing_Spots/replication/figures/ExpertSameOther.pdf", width=8, height=4, pointsize=10)


rm(list=ls())  

library(foreign)
library(boot)
library(gplots)
library(ggplot2)
library(car)
library(survey)
library(grDevices)
library(plotrix)

# Caterpilar plots                                             
load('~/Dropbox/Seeing_Spots/replication/expert_opinion.Rdata')
   
DATA$Average_1 <- ((100-DATA$SkipPer_1)+DATA$Rewatch_1+DATA$Share_1+DATA$GetLink_1)/4
DATA$Average_2 <- ((100-DATA$SkipPer_2)+DATA$Rewatch_2+DATA$Share_2+DATA$GetLink_2)/4
DATA$Average_3 <- ((100-DATA$SkipPer_3)+DATA$Rewatch_3+DATA$Share_3+DATA$GetLink_3)/4
DATA$Average_4 <- ((100-DATA$SkipPer_4)+DATA$Rewatch_4+DATA$Share_4+DATA$GetLink_4)/4
             
      

#Estimates
Est.SkipPosOwn <- NULL
Est.SkipPosOther <- NULL
Est.SkipNegOwn <- NULL
Est.SkipNegOther <- NULL

Est.RewatchPosOwn <- NULL
Est.RewatchPosOther <- NULL
Est.RewatchNegOwn <- NULL
Est.RewatchNegOther <- NULL

Est.SharePosOwn <- NULL
Est.SharePosOther <- NULL
Est.ShareNegOwn <- NULL
Est.ShareNegOther <- NULL

Est.GetLinkPosOwn <- NULL
Est.GetLinkPosOther <- NULL
Est.GetLinkNegOwn <- NULL
Est.GetLinkNegOther <- NULL

Est.AveragePosOwn <- NULL
Est.AveragePosOther <- NULL
Est.AverageNegOwn <- NULL
Est.AverageNegOther <- NULL

# bootstrap means
for (b in 1:10000) {
	
#Skipping
  # Sampling and Estimates
  SkipPosOwn.star = sample(DATA$SkipPer_1, size=length(DATA$SkipPer_1), replace=TRUE)
  Est.SkipPosOwn <- c(Est.SkipPosOwn, mean(100-SkipPosOwn.star, na.rm=T))
  
  SkipPosOther.star = sample(DATA$SkipPer_2, size=length(DATA$SkipPer_2), replace=TRUE)
  Est.SkipPosOther <- c(Est.SkipPosOther, mean(100-SkipPosOther.star, na.rm=T))
      
  SkipNegOwn.star = sample(DATA$SkipPer_3, size=length(DATA$SkipPer_3), replace=TRUE)
  Est.SkipNegOwn <- c(Est.SkipNegOwn, mean(100-SkipNegOwn.star, na.rm=T))
  
  SkipNegOther.star = sample(DATA$SkipPer_4, size=length(DATA$SkipPer_4), replace=TRUE)
  Est.SkipNegOther <- c(Est.SkipNegOther, mean(100-SkipNegOther.star, na.rm=T))
  
#Rewatch
  # Sampling and Estimates
  RewatchPosOwn.star = sample(DATA$Rewatch_1, size=length(DATA$Rewatch_1), replace=TRUE)
  Est.RewatchPosOwn <- c(Est.RewatchPosOwn, mean(RewatchPosOwn.star, na.rm=T))
  
  RewatchPosOther.star = sample(DATA$Rewatch_2, size=length(DATA$Rewatch_2), replace=TRUE)
  Est.RewatchPosOther <- c(Est.RewatchPosOther, mean(RewatchPosOther.star, na.rm=T))
      
  RewatchNegOwn.star = sample(DATA$Rewatch_3, size=length(DATA$Rewatch_3), replace=TRUE)
  Est.RewatchNegOwn <- c(Est.RewatchNegOwn, mean(RewatchNegOwn.star, na.rm=T))
  
  RewatchNegOther.star = sample(DATA$Rewatch_4, size=length(DATA$Rewatch_4), replace=TRUE)
  Est.RewatchNegOther <- c(Est.RewatchNegOther, mean(RewatchNegOther.star, na.rm=T))

#Share
  # Sampling and Estimates
  SharePosOwn.star = sample(DATA$Share_1, size=length(DATA$Share_1), replace=TRUE)
  Est.SharePosOwn <- c(Est.SharePosOwn, mean(SharePosOwn.star, na.rm=T))
  
  SharePosOther.star = sample(DATA$Share_2, size=length(DATA$Share_2), replace=TRUE)
  Est.SharePosOther <- c(Est.SharePosOther, mean(SharePosOther.star, na.rm=T))
      
  ShareNegOwn.star = sample(DATA$Share_3, size=length(DATA$Share_3), replace=TRUE)
  Est.ShareNegOwn <- c(Est.ShareNegOwn, mean(ShareNegOwn.star, na.rm=T))
  
  ShareNegOther.star = sample(DATA$Share_4, size=length(DATA$Share_4), replace=TRUE)
  Est.ShareNegOther <- c(Est.ShareNegOther, mean(ShareNegOther.star, na.rm=T))

#GetLink
  # Sampling and Estimates
  GetLinkPosOwn.star = sample(DATA$GetLink_1, size=length(DATA$GetLink_1), replace=TRUE)
  Est.GetLinkPosOwn <- c(Est.GetLinkPosOwn, mean(GetLinkPosOwn.star, na.rm=T))
  
  GetLinkPosOther.star = sample(DATA$GetLink_2, size=length(DATA$GetLink_2), replace=TRUE)
  Est.GetLinkPosOther <- c(Est.GetLinkPosOther, mean(GetLinkPosOther.star, na.rm=T))
      
  GetLinkNegOwn.star = sample(DATA$GetLink_3, size=length(DATA$GetLink_3), replace=TRUE)
  Est.GetLinkNegOwn <- c(Est.GetLinkNegOwn, mean(GetLinkNegOwn.star, na.rm=T))
  
  GetLinkNegOther.star = sample(DATA$GetLink_4, size=length(DATA$GetLink_4), replace=TRUE)
  Est.GetLinkNegOther <- c(Est.GetLinkNegOther, mean(GetLinkNegOther.star, na.rm=T))


#Average
  # Sampling and Estimates
  AveragePosOwn.star = sample(DATA$Average_1, size=length(DATA$Average_1), replace=TRUE)
  Est.AveragePosOwn <- c(Est.AveragePosOwn, mean(AveragePosOwn.star, na.rm=T))
  
  AveragePosOther.star = sample(DATA$Average_2, size=length(DATA$Average_2), replace=TRUE)
  Est.AveragePosOther <- c(Est.AveragePosOther, mean(AveragePosOther.star, na.rm=T))
      
  AverageNegOwn.star = sample(DATA$Average_3, size=length(DATA$Average_3), replace=TRUE)
  Est.AverageNegOwn <- c(Est.AverageNegOwn, mean(AverageNegOwn.star, na.rm=T))
  
  AverageNegOther.star = sample(DATA$Average_4, size=length(DATA$Average_4), replace=TRUE)
  Est.AverageNegOther <- c(Est.AverageNegOther, mean(AverageNegOther.star, na.rm=T))
 
}


ex=list(
DATA$GetLink_1, DATA$GetLink_2, DATA$GetLink_3, DATA$GetLink_4,
DATA$Share_1, DATA$Share_2, DATA$Share_3, DATA$Share_4,
DATA$Rewatch_1, DATA$Rewatch_2, DATA$Rewatch_3, DATA$Rewatch_4,
DATA$SkipPer_1, DATA$SkipPer_2, DATA$SkipPer_3, DATA$SkipPer_4,
DATA$Average_1, DATA$Average_2, DATA$Average_3, DATA$Average_4
)

x=list(
Est.GetLinkPosOwn, Est.GetLinkPosOther, Est.GetLinkNegOwn, Est.GetLinkNegOther,
Est.SharePosOwn, Est.SharePosOther, Est.ShareNegOwn, Est.ShareNegOther,
Est.RewatchPosOwn, Est.RewatchPosOther, Est.RewatchNegOwn, Est.RewatchNegOther,
Est.SkipPosOwn, Est.SkipPosOther, Est.SkipNegOwn, Est.SkipNegOther,
Est.AveragePosOwn, Est.AveragePosOther, Est.AverageNegOwn, Est.AverageNegOther
)

xlen <- length(x)
segs <- matrix(0, nrow = 4, ncol = xlen)
    for (i in 1:xlen) {
        segs[1, i] <- mean(x[[i]])
        segs[2, i] <- quantile(x[[i]], .025)
        segs[3, i] <- quantile(x[[i]], .975)
        segs[4, i] <- length(which(complete.cases(ex[[i]])))
    }
    rownames(segs) <- c("Estimate", "Quantile .025", "Quantile .975", "N")
    colnames(segs) <- c(
    "Positive, Respondent's Party ", "Positive, Other Party ", "Negative, Respondent's Party ", "Negative, Other Party ",
    "Positive, Respondent's Party ", "Positive, Other Party ", "Negative, Respondent's Party ", "Negative, Other Party ",
    "Positive, Respondent's Party ", "Positive, Other Party ", "Negative, Respondent's Party ", "Negative, Other Party ",
    "Positive, Respondent's Party ", "Positive, Other Party ", "Negative, Respondent's Party ", "Negative, Other Party ",
    "Positive, Respondent's Party ", "Positive, Other Party ", "Negative, Respondent's Party ", "Negative, Other Party "
    )

    
centipede.plot(segs, sort.segs=FALSE, main="Mean Expert Estimates for Ad Seeking Behaviors", 
	xlab="Bootstrapped Means and 95 Percent Confidence Intervals", vgrid=0, mar=c(5,12,3,5), lty=1, 
	right.labels=paste(round(segs[1, ], 2), " (", segs[4, ], ")", sep = ""), bg="grey", xlim=c(-2.5,81), col="black")
abline(v=50, lty=2, col="grey")
abline(h=c(4.5, 8.5, 12.5, 16.5), col="black")
text( c(-2.5,-2.5,-2.5,-2.5,-2.5) ,c(2.5,6.5,10.5,14.5,18.5), labels=c("Get Link", "Share", "Replay", "Not Skip", "Overall"), srt=90)
 


dev.print(pdf, file="~/Dropbox/Seeing_Spots/replication/figures/Expert_Seeking_Interactions.pdf", width=11, pointsize=12)


#END figure_ii_iii